package dao;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import util.HibernateSessionFactoryUtil;

public class BasicDao {
	
		/**
		 * object:ʵ�����
		 * ���ܣ��������
		 */
		public void save(Object object) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction tx=session.beginTransaction();
			try {
			session.save(object);
			tx.commit();
			} catch (Exception e) {
			e.printStackTrace();
			}finally{
			HibernateSessionFactoryUtil.closeSession(session);
			}

		}
		
		/**
		 * @param object Ҫɾ��Ķ���
		 */
		public void delete(Object object) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction tx=session.beginTransaction();
			try {
				session.delete(object);
				tx.commit();
			} catch (Exception ex) {
				ex.printStackTrace();
				tx.rollback();
			}
			session.close();

		}
		
		/**
		 * object:ʵ�����
		 * ���ܣ����¶�����Ϣ��
		 */
		public void update(Object object) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction transation=session.beginTransaction();
			try {
				session.saveOrUpdate(object);
				transation.commit();
			} catch (Exception e) {
				e.printStackTrace();
				transation.rollback();
			}finally{
				HibernateSessionFactoryUtil.closeSession(session);
			}	
		}
		
		/**
		 * objectName:ʵ�������ơ�
		 * id:��š�
		 * ���ܣ�����id���Ҿ������
		 */
		public Object getObjectById(String objectName,int id) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction tx=session.beginTransaction();
			List<Object> list=null;
			try {
				Query query=session.createQuery("from "+objectName+" where Id=?");
				query.setInteger(0, id);
				list= query.list();
	     		 tx.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally{
			HibernateSessionFactoryUtil.closeSession(session); 
			}
			return list.get(0);
		}
		
		/**
		 * objectName:ʵ�������ơ�
		 * name:�û���ѧ�Ż��ʦ���ţ���
		 * ���ܣ�����û���鿴������Ϣ��
		 */
		public List<Object> getObjectByName(String objectName,String name) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction tx=session.beginTransaction();
			List<Object> list=null;
			try {
				Query query=session.createQuery("from "+objectName+" where name=?");
				query.setString(0,name);
				list= query.list();
	     		 tx.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally{
			HibernateSessionFactoryUtil.closeSession(session); 
			}
			return list;
		}
		
		
		/**
		 * objectName:ʵ�������ơ�
		 * ���ܣ�����ʵ����Ʋ���������ݡ�
		 */
		@SuppressWarnings("unchecked")
		public List<Object> getAllObjects(String objectName) {
			Session session=HibernateSessionFactoryUtil.getSession();
			Transaction tx=session.beginTransaction();
			List<Object> list=null;
			try {
				Query query=session.createQuery("from "+objectName);
				 list=query.list();
	     		 tx.commit(); 
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally{
			HibernateSessionFactoryUtil.closeSession(session); 
			}
			return list;
		}
		
		
}
